import org.dom4j.DocumentException;
import org.junit.Before;
import org.junit.Test;
import wsz.IUserDao;
import wsz.User;
import wsz.io.Resource;
import wsz.sqlSession.SqlSession;
import wsz.sqlSession.SqlSessionFactory;
import wsz.sqlSession.SqlSessionFactoryBuilder;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

/**
 * @author wsz
 * @date 2021/10/1 0:35
 **/
public class ApiTest {

    private SqlSession sqlSession;

    @Before
    public void init() throws PropertyVetoException, DocumentException {
        InputStream inputStream = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = factory.openSession();
    }

    /**
     * 根据statementId获取目标sql
     */
    @Test
    public void testStatement() {

        User user = new User();
        user.setId(1);
        user.setName("张三");
        User one = sqlSession.selectOne("user.selectOne", user);
        System.out.println(one);

        List<User> objects = sqlSession.selectList("user.selectList");
        System.out.println(objects);
    }

    /**
     * xml的namespace指向接口
     * @throws Exception
     */
    @Test
    public void mapperTest() throws Exception {
        User user = new User();
        user.setId(1);
        user.setName("张三");

        IUserDao mapper = sqlSession.getMapper(IUserDao.class);

        List<User> byCondition = mapper.findByCondition(user);
        System.out.println(byCondition);

        List<User> all = mapper.findAll();
        System.out.println(all);
    }
}
